package com.pllandxzx.service.impl;

import com.google.common.util.concurrent.AtomicDouble;
import com.pll.dto.ResponseDto;
import com.pll.pojo.Order;
import com.pllandxzx.mapper.HomeMapper;
import com.pllandxzx.service.HomeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HomeServiceImpl
 * @Description TODO
 * @Author 小鹿
 * @Date 2025/6/30 17:03
 */
@Service
@RequiredArgsConstructor
public class HomeServiceImpl implements HomeService
{
    private final HomeMapper homeMapper;

    @Override
    public ResponseDto getAllOrderData()
    {
        // 获取当前日期的订单量
        LocalDate currentDate = LocalDate.now();
        System.out.println("当前日期（LocalDate）: " + currentDate);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);
        System.out.println("格式化后的日期: " + formattedDate);
        Integer nowAllOrderData = homeMapper.getAllOrderData(formattedDate);
        System.out.println("今天的订单量" + nowAllOrderData);

        // 获取前一天的日期所有订单
        LocalDate yesterday = LocalDate.now().minusDays(1);
        String forYesterdayDate = yesterday.format(formatter);
        Integer yesterdayAllOrderDate = homeMapper.getAllOrderData(forYesterdayDate);
        System.out.println("昨天的订单量" + yesterdayAllOrderDate);

        //获取今日订单
        String nowAllDrawing = getAllDrawing(formattedDate);
        System.out.println("今日商家抽成" + nowAllDrawing);
        String yesterdayAllDrawing = getAllDrawing(forYesterdayDate);
        System.out.println("昨日商家抽成" + yesterdayAllDrawing);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("nowAllOrderData",nowAllOrderData);
        map.put("yesterdayAllOrderDate",yesterdayAllOrderDate);
        map.put("allOrderDrawing",nowAllDrawing);
        map.put("yesterdayAllDrawing",yesterdayAllDrawing);

        //近七日销售趋势 获取近七日数据
        LocalDate sevenDaysAgo = LocalDate.now().minusDays(7);
        List<Map<String, Object>> sevenDaysData = homeMapper.getDailyOrderAmount(sevenDaysAgo, currentDate);
        System.out.println("近七日数据:"+sevenDaysData);
        map.put("sevenDaysData",sevenDaysData);

        //获取订单状态分布
        List<Map<String, Object>> orderStatusCount = homeMapper.getOrderStatusCount();
        System.out.println("订单状态分布"+orderStatusCount);
        map.put("orderStatusCount",orderStatusCount);
        return ResponseDto.success(map);
    }

    private String getAllDrawing(String formattedDate)
    {
        List<Order> todayOrder =homeMapper.getTodayOrder(formattedDate);
        //获取商家抽成
        AtomicDouble allOrderDrawing = new AtomicDouble(0.0);
        todayOrder.forEach(order -> {
            Double merchantsDraw = homeMapper.getMerchantsDraw(order.getMerId());
            double orderDrawing = order.getActualPrice() * merchantsDraw;
            allOrderDrawing.addAndGet(orderDrawing);
        });
        DecimalFormat df2 = new DecimalFormat("0.00");
        df2.setRoundingMode(java.math.RoundingMode.HALF_UP); // 四舍五入
        return df2.format(allOrderDrawing.get());
    }
}